﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AjaxControlFramework
{
    [AttributeUsage(AttributeTargets.Property)]
    public class AjaxControlPropertyAttribute : Attribute
    {
        //------// Properties \\--------------------------------------------\\
        public virtual string Name { get; set; }
        public virtual object DefaultValue { get; set; }


        private bool? _updateOnCallback = null;
        public virtual bool? UpdateOnCallback
        {
            get { return _updateOnCallback; }
            set { _updateOnCallback = value; }
        }


        private string[] _authorizedRoles = null;
        public virtual string[] AuthorizedRoles
        {
            get { return _authorizedRoles; }
            set
            {
                _authorizedRoles = value;

                if (_authorizedRoles != null)
                {
                    ValidateAuthorizedRoles(_authorizedRoles);
                }
            }
        }


        public virtual bool Protected
        {
            get { return (AuthorizedRoles != null && AuthorizedRoles.Length > 0); }
        }
        //------\\ Properties //--------------------------------------------//



        //------// Constructors \\------------------------------------------\\
        public AjaxControlPropertyAttribute() 
        { }


        public AjaxControlPropertyAttribute(string name)
        {
            Name = name;
        }


        public AjaxControlPropertyAttribute(string name, params string[] authorizedRoles)
            : this(name)
        {
            AuthorizedRoles = authorizedRoles;
        }


        public AjaxControlPropertyAttribute(bool updateOnCallback)
        {
            UpdateOnCallback = updateOnCallback;
        }


        public AjaxControlPropertyAttribute(object defaultValue)
        {
            DefaultValue = defaultValue;
        }


        public AjaxControlPropertyAttribute(object defaultValue, params string[] authorizedRoles)
            : this(defaultValue)
        {
            AuthorizedRoles = authorizedRoles;
        }


        public AjaxControlPropertyAttribute(object defaultValue, bool updateOnCallback)
        {
            DefaultValue = defaultValue;
            UpdateOnCallback = updateOnCallback;
        }


        public AjaxControlPropertyAttribute(object defaultValue, bool updateOnCallback, params string[] authorizedRoles)
            : this(defaultValue, updateOnCallback)
        {
            AuthorizedRoles = authorizedRoles;
        }


        public AjaxControlPropertyAttribute(string name, object defaultValue)
        {
            Name = name;
            DefaultValue = defaultValue;
        }


        public AjaxControlPropertyAttribute(string name, object defaultValue, params string[] authorizedRoles)
            : this(name, defaultValue)
        {
            AuthorizedRoles = authorizedRoles;
        }


        public AjaxControlPropertyAttribute(string name, object defaultValue, bool updateOnCallback)
        {
            Name = name;
            DefaultValue = defaultValue;
            UpdateOnCallback = updateOnCallback;
        }


        public AjaxControlPropertyAttribute(string name, object defaultValue, bool updateOnCallback, params string[] authorizedRoles)
            : this(name, defaultValue, updateOnCallback)
        {
            AuthorizedRoles = authorizedRoles;
        }
        //------\\ Constructors //------------------------------------------//



        //------// Methods \\-----------------------------------------------\\
        protected virtual void ValidateAuthorizedRoles(string[] authorizedRoles)
        {
            if (authorizedRoles == null) { return; }

            foreach (string authorizedRole in authorizedRoles)
            {
                if (String.IsNullOrWhiteSpace(authorizedRole))
                {
                    throw new InvalidUserRolesException("The array of authorized roles contain one or more null/empty string values.");
                }
            }
        }
        //------\\ Methods //-----------------------------------------------//
    }
}
